package com.bindatax.biz.etl.create;

import com.bindatax.core.custom.GlobalConstant;
import com.bindatax.core.meta.TaskMeta;
import com.bindatax.core.util.PropertiesUtil;
import com.bindatax.core.util.StringUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Properties;
import java.util.Scanner;
import java.util.Set;

public class CreateJavaCode {
    public static void start(String configPath){
        if (configPath == null) {
            System.out.println("程序错误！！！！无法找到配置文件目录！");
            return;
        }
        File configFile = new File(configPath);
        if (!configFile.exists()) {
            System.out.println("程序错误！！！！无法找到配置文件目录！");
            return;
        }
        configPath = configFile.getAbsolutePath();
        configFile = new File(configPath);

        String java_script_txt = configFile.getParentFile().getAbsolutePath() +"/java_script_txt";

        System.out.println("生成java片段组件代码程序");
        String inputValue;
        Scanner input = new Scanner(System.in); // 创建对象，从标准输入读取

        // 分别加载四个配置文件对象
        String TASK_DS_POOL_FILE_NAME = configPath +"/"+ GlobalConstant.TASK_DS_POOL_FILE_NAME;//线程池配置文件
        String TASK_LOG_FILE_NAME = configPath +"/"+ GlobalConstant.TASK_LOG_FILE_NAME;//日志配置文件
        String TASK_META_FILE_NAME = configPath +"/"+ GlobalConstant.TASK_META_FILE_NAME;//任务元数据配置文件
        String TASK_SYS_FILE_NAME= configPath +"/"+ GlobalConstant.TASK_SYS_FILE_NAME;//任务系统配置文件

        System.out.println("\t线程池默认配置文件：" + TASK_DS_POOL_FILE_NAME);
        System.out.println("\t日志默认配置文件：" + TASK_LOG_FILE_NAME);
        System.out.println("\t任务元数据默认配置文件：" + TASK_META_FILE_NAME);
        System.out.println("\t系统级别默认配置文件：" + TASK_SYS_FILE_NAME);

        Properties taskMetaPros = PropertiesUtil.read(TASK_META_FILE_NAME);
        Properties fieldPro = new Properties();
        System.out.println("--------------------------------------");
        System.out.println("第2项配置：配置日志库连接信息，请选择：");
        System.out.println("\t1-使用task-meta.properties里面的[db.log.]前缀配置信息。");
        System.out.println("\t2-手动配置");
        System.out.println("\t3-从上级字段获取");
        inputValue = StringUtils.getValue(input.nextLine());
        if ("2".equals(inputValue)) {
            System.out.println("选择2：手动配置。");
            System.out.println("\t输入日志库 JDBC驱动");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_logDriver,inputValue);

            System.out.println("\t输入日志库 URL：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_logUrl,inputValue);

            System.out.println("\t输入日志库 用户名：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_logUser,inputValue);

            System.out.println("\t输入日志库 密码：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_logPwd,inputValue);

            System.out.println("\t输入日志库 Schema：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_logSchema,inputValue);

            System.out.println("\t输入日志库 连接校验SQL语句：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_logValidationQuery,inputValue);
        }if("3".equals(inputValue)){
            System.out.println("选择3：从上级字段获取。");
            System.out.println("\t输入日志库 JDBC驱动 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_logDriver,inputValue);

            System.out.println("\t输入日志库 URL 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_logUrl,inputValue);

            System.out.println("\t输入日志库 用户名 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_logUser,inputValue);

            System.out.println("\t输入日志库 密码 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_logPwd,inputValue);

            System.out.println("\t输入日志库 Schema 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_logSchema,inputValue);

            System.out.println("\t输入日志库 连接校验SQL语句 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_logValidationQuery,inputValue);
        }else {
            System.out.println("选择1：使用task-meta.properties里面的[db.log.]前缀配置信息。");
        }
        System.out.println("--------------------------------------");
        System.out.println("第3项配置：配置源库连接信息，请选择：");
        System.out.println("\t1-使用task-meta.properties里面的[db.source.]前缀配置信息。");
        System.out.println("\t2-手动配置");
        System.out.println("\t3-从上级字段获取");
        inputValue = StringUtils.getValue(input.nextLine());
        if ("2".equals(inputValue)) {
            System.out.println("选择2：手动配置。");
            System.out.println("\t输入日志库 JDBC驱动：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_sourceDriver,inputValue);

            System.out.println("\t输入源库 URL：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_sourceUrl,inputValue);

            System.out.println("\t输入源库 用户名：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_sourceUser,inputValue);

            System.out.println("\t输入源库 密码：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_sourcePwd,inputValue);

            System.out.println("\t输入源库 Schema：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_sourceSchema,inputValue);

            System.out.println("\t输入源库 连接校验SQL语句：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_sourceValidationQuery,inputValue);
        }if("3".equals(inputValue)){
            System.out.println("选择3：从上级字段获取。");
            System.out.println("\t输入源库 JDBC驱动 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_sourceDriver,inputValue);

            System.out.println("\t输入源库 URL 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_sourceUrl,inputValue);

            System.out.println("\t输入源库 用户名 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_sourceUser,inputValue);

            System.out.println("\t输入源库 密码 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_sourcePwd,inputValue);

            System.out.println("\t输入源库 Schema 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_sourceSchema,inputValue);

            System.out.println("\t输入源库 连接校验SQL语句 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_sourceValidationQuery,inputValue);
        }else {
            System.out.println("选择1：使用task-meta.properties里面的[db.source.]前缀配置信息。");
        }
        System.out.println("--------------------------------------");
        System.out.println("第4项配置：配置目标库连接信息，请选择：");
        System.out.println("\t1-使用task-meta.properties里面的[db.target.]前缀配置信息。");
        System.out.println("\t2-手动配置");
        System.out.println("\t3-从上级字段获取");
        inputValue = StringUtils.getValue(input.nextLine());
        if ("2".equals(inputValue)) {
            System.out.println("选择2：手动配置。");
            System.out.println("\t输入目标库 JDBC驱动：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_targetDriver,inputValue);

            System.out.println("\t输入目标库 URL：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_targetUrl,inputValue);

            System.out.println("\t输入目标库 用户名：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_targetUser,inputValue);

            System.out.println("\t输入目标库 密码：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_targetPwd,inputValue);

            System.out.println("\t输入目标库 Schema：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_targetSchema,inputValue);

            System.out.println("\t输入目标库 连接校验SQL语句：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_targetValidationQuery,inputValue);
        }if("3".equals(inputValue)){
            System.out.println("选择3：从上级字段获取。");
            System.out.println("\t输入目标库 JDBC驱动 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_targetDriver,inputValue);

            System.out.println("\t输入目标库 URL 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_targetUrl,inputValue);

            System.out.println("\t输入目标库 用户名 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_targetUser,inputValue);

            System.out.println("\t输入目标库 密码 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_targetPwd,inputValue);

            System.out.println("\t输入目标库 Schema 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_targetSchema,inputValue);

            System.out.println("\t输入目标库 连接校验SQL语句 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_targetValidationQuery,inputValue);
        }else {
            System.out.println("选择1：使用task-meta.properties里面的[db.source.]前缀配置信息。");
        }
        System.out.println("--------------------------------------");
        System.out.println("第5项配置：任务配置信息，请选择：");
        System.out.println("\t1-使用task-meta.properties里面的相关配置信息。");
        System.out.println("\t2-手动配置");
        System.out.println("\t3-从上级字段获取");
        inputValue = StringUtils.getValue(input.nextLine());
        if ("2".equals(inputValue)) {
            System.out.println("选择2：手动配置。");

            System.out.println("\t输入查询SQL语句：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_querySql,inputValue);

            System.out.println("\t输入源库 数据源schema：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_querySchema,inputValue);

            System.out.println("\t输入 数据源查询结果-增量时间字段(如果没有，则不填写)：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_queryIncrementTimeField,inputValue);

            System.out.println("\t输入目标库 目标表名：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_targetTableName,inputValue);

            System.out.println("\t输入目标表主键字段名（若存在多个字段成联合主键，则以英文逗号分隔）：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_targetKeys,inputValue);

            System.out.println("\t输入是否自动检测目标表（y/n）");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_targeAutoCheckKey,inputValue);

            System.out.println("\t输入 查询结果数据的入库数据段数量：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_writeSegmentNumber,inputValue);

            System.out.println("\t输入 入库批次提交大小（每次提交多少条）：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_writeSegmentBatchSize,inputValue);

            System.out.println("\t输入 入库超时时间（秒）：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_writeAwaitOutTime,inputValue);

            System.out.println("\t输入 是否忽略入库错误继续执行（y/n）：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_errorIgnore,inputValue);

            System.out.println("\t输入 最多输出多少条报错数据：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_logOutErrorMaxSize,inputValue);

            System.out.println("\t输入 是否开启总数统计（y/n）：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_openTotalStatistics,inputValue);

            System.out.println("\t输入 某批次的数据入库失败后，重试多少次入库(单位是次)：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_failTryNum,inputValue);

            System.out.println("\t输入 某批次的数据入库失败后，重试入库的时间间隔（单位是秒）：");
            inputValue = StringUtils.getValue(input.nextLine());
            taskMetaPros.put(TaskMeta.key_failTryInterval,inputValue);

        }if("3".equals(inputValue)){
            System.out.println("选择3：从上级字段获取。");

            System.out.println("\t输入查询SQL语句 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_querySql,inputValue);

            System.out.println("\t输入源库 数据源schema 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_querySchema,inputValue);

            System.out.println("\t输入 数据源查询结果-增量时间字段(如果没有，则不填写) 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_queryIncrementTimeField,inputValue);

            System.out.println("\t输入目标库 目标表名 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_targetTableName,inputValue);

            System.out.println("\t输入目标表主键字段名（若存在多个字段成联合主键，则以英文逗号分隔） 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_targetKeys,inputValue);

            System.out.println("\t输入是否自动检测目标表（y/n） 字段名");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_targeAutoCheckKey,inputValue);

            System.out.println("\t输入 查询结果数据的入库数据段数量 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_writeSegmentNumber,inputValue);

            System.out.println("\t输入 入库批次提交大小（每次提交多少条）字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_writeSegmentBatchSize,inputValue);

            System.out.println("\t输入 入库超时时间（秒）字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_writeAwaitOutTime,inputValue);

            System.out.println("\t输入 是否忽略入库错误继续执行（y/n）字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_errorIgnore,inputValue);

            System.out.println("\t输入 最多输出多少条报错数据 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_logOutErrorMaxSize,inputValue);

            System.out.println("\t输入 是否开启总数统计（y/n）字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_openTotalStatistics,inputValue);

            System.out.println("\t输入 某批次的数据入库失败后，重试多少次入库(单位是次) 字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_failTryNum,inputValue);

            System.out.println("\t输入 某批次的数据入库失败后，重试入库的时间间隔（单位是秒）字段名：");
            inputValue = StringUtils.getValue(input.nextLine());
            fieldPro.put(TaskMeta.key_failTryInterval,inputValue);

        }else {
            System.out.println("选择1：使用task-meta.properties里面的相关配置信息。");
        }
        System.out.println("--------------------------------------");
        System.out.println("第6项配置(选填)：业务级别字段配置");
        boolean run = true;
        while (run){
            System.out.println("\t请选择 1-从上游字段名取值；2-手动填写字段名和值；3-退出当前的配置流程[第6项]：");
            inputValue = StringUtils.getValue(input.nextLine());
            switch (inputValue){
                case "1":{
                    System.out.println("\t\t请输入任务配置属性名：");
                    String name = StringUtils.getValue(input.nextLine());
                    System.out.println("\t\t请输入任务配置属性名对应的上游字段名：");
                    inputValue = StringUtils.getValue(input.nextLine());
                    System.out.println("输入的属性名 / 上游字段名 为：" + name +" / " + inputValue);
                    fieldPro.put(TaskMeta.BUSINESS_ATTR_PREFIX + name,inputValue);
                    break;
                }
                case "2":{
                    System.out.println("\t\t请输入任务配置属性名：");
                    String name = StringUtils.getValue(input.nextLine());
                    System.out.println("\t\t请输入任务配置值：");
                    inputValue = StringUtils.getValue(input.nextLine());
                    System.out.println("输入的属性名 / 配置值 为：" + name +" / " + inputValue);
                    taskMetaPros.put(TaskMeta.BUSINESS_ATTR_PREFIX + name,inputValue);
                    break;
                }
                case "3":{
                    System.out.println("\t退出配置业务属性字段流程。");
                    run = false;
                    break;
                }
            }
        }

        Properties dsPoolPros = PropertiesUtil.read(TASK_DS_POOL_FILE_NAME);
        Properties logPros = PropertiesUtil.read(TASK_LOG_FILE_NAME);
        Properties sysPros = PropertiesUtil.read(TASK_SYS_FILE_NAME);
        String javaTxt = create(fieldPro, taskMetaPros, sysPros, dsPoolPros, logPros);

        writeTxt(javaTxt,java_script_txt +"/"+ System.currentTimeMillis() + ".txt");
    }

    public static void writeTxt(String content,String path){
        File file = new File(path);
        File parent = file.getParentFile();
        if(!parent.exists()){
            parent.mkdirs();
        }
        path = file.getAbsolutePath();
        System.out.println("java程序片段文本文件路径为：" + path);
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))) {
            writer.write(content);
        } catch (Exception e) {
            System.err.println("写入文件时发生错误: " + e.getMessage());
        }
    }


    public final static String IMPORT_BODY = "#IMPORT_BODY#";
    public final static String ATTRIBUTE_BODY = "#ATTRIBUTE_BODY#";//属性区域
    public final static String OUTPUT_DONE_BODY = "#OUTPUT_DONE_BODY#";
    public final static String FIRST_BODY = "#FIRST_BODY#";
    public final static String EXE_ROW_BODY = "#EXE_ROW_BODY#";

    private static String create(Properties fieldPro,Properties taskMetaPros,Properties sysPros,Properties dsPoolPros,Properties logPros){
        StringBuilder javaTxtSb = new StringBuilder();
        javaTxtSb.append(IMPORT_BODY).append(GlobalConstant.CR);
        javaTxtSb.append(ATTRIBUTE_BODY).append(GlobalConstant.CR);
        javaTxtSb.append("public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException{ ").append(GlobalConstant.CR);
        javaTxtSb.append("\t\tObject[] r = getRow();").append(GlobalConstant.CR);
        javaTxtSb.append("\t\tif (r == null) { ").append(GlobalConstant.CR);
        javaTxtSb.append("\t\t\t").append(OUTPUT_DONE_BODY).append(GlobalConstant.CR);
        javaTxtSb.append("\t\t\tsetOutputDone();").append(GlobalConstant.CR);
        javaTxtSb.append("\t\t\treturn false;").append(GlobalConstant.CR);
        javaTxtSb.append("\t\t}").append(GlobalConstant.CR);
        javaTxtSb.append("\t\tif (first){ ").append(GlobalConstant.CR);
        javaTxtSb.append("\t\t\tfirst = false;").append(GlobalConstant.CR);
        javaTxtSb.append(FIRST_BODY).append(GlobalConstant.CR);
        javaTxtSb.append("\t\t}").append(GlobalConstant.CR);
        javaTxtSb.append("\t\t").append(EXE_ROW_BODY).append(GlobalConstant.CR);
        javaTxtSb.append("\t\tputRow(data.outputRowMeta, r);").append(GlobalConstant.CR);
        javaTxtSb.append("\t\treturn true;").append(GlobalConstant.CR);
        javaTxtSb.append("}").append(GlobalConstant.CR);
        String javaTxt = javaTxtSb.toString();

        StringBuilder importSb = new StringBuilder();
        importSb.append("import java.util.*;").append(GlobalConstant.CR);
        importSb.append("import com.bindatax.biz.etl.BinDataxBoot;").append(GlobalConstant.CR);
        importSb.append("import com.bindatax.core.task.DataTaskPool;").append(GlobalConstant.CR);
        importSb.append("import com.bindatax.core.util.StringUtils;").append(GlobalConstant.CR);
        javaTxt = javaTxt.replace(IMPORT_BODY,importSb.toString());

        StringBuilder attributeSb = new StringBuilder();
        attributeSb.append("private Properties sysPros = null;").append(GlobalConstant.CR);
        attributeSb.append("private Properties dsPoolPros = null;").append(GlobalConstant.CR);
        attributeSb.append("private Properties logPros = null;").append(GlobalConstant.CR);
        attributeSb.append("private DataTaskPool dataTaskPool = null;").append(GlobalConstant.CR);
        attributeSb.append("private String configValue = null;").append(GlobalConstant.CR);
        javaTxt = javaTxt.replace(ATTRIBUTE_BODY,attributeSb.toString());

        StringBuilder outputDoneSb = new StringBuilder();
        outputDoneSb.append("if( dataTaskPool != null ){ // 上级组件执行完成时，等待任务组执行结束").append(GlobalConstant.CR);
        outputDoneSb.append("\t\t\t\tdataTaskPool.waitDataTaskFinished();").append(GlobalConstant.CR);
        outputDoneSb.append("\t\t\t\tdataTaskPool = null;").append(GlobalConstant.CR);
        outputDoneSb.append("\t\t\t}").append(GlobalConstant.CR);
        javaTxt = javaTxt.replace(OUTPUT_DONE_BODY,outputDoneSb.toString());

        String firstBody;
        StringBuilder sysProsTxt = new StringBuilder();
        sysProsTxt.append("\t\t\t// 系统配置").append(GlobalConstant.CR);
        sysProsTxt.append("\t\t\tsysPros = new Properties();").append(GlobalConstant.CR);
        Set<String> sysProsNames = sysPros.stringPropertyNames();
        for (String name : sysProsNames){
            if("trans.concurrency.query.minSize".equalsIgnoreCase(name)){
                sysProsTxt.append("\t\t\t// 同时执行多少条动态SQL语句").append(GlobalConstant.CR);
                sysProsTxt.append("\t\t\tconfigValue = get(Fields.In, \"concurrencyQueryMinSize\").getString(r);").append(GlobalConstant.CR);
                sysProsTxt.append("\t\t\tconfigValue = StringUtils.getValue(configValue,\"5\");").append(GlobalConstant.CR);
                sysProsTxt.append("\t\t\tsysPros.put( \"trans.concurrency.query.minSize\" , configValue );").append(GlobalConstant.CR);
            }else {
                String value = sysPros.getProperty(name);
                sysProsTxt.append("\t\t\tsysPros.put( \""+name+"\" ,  \""+value+"\" );").append(GlobalConstant.CR);
            }
        }
        firstBody = sysProsTxt.toString();

        StringBuilder dsPoolProsTxt = new StringBuilder();
        dsPoolProsTxt.append("\t\t\tdsPoolPros = new Properties();// 数据源配置\n");
        Set<String> dsPoolProsNames = dsPoolPros.stringPropertyNames();
        for (String name : dsPoolProsNames){
            String value = dsPoolPros.getProperty(name);
            dsPoolProsTxt.append("\t\t\tdsPoolPros.put( \""+name+"\" ,  \""+value+"\" );").append(GlobalConstant.CR);
        }
        firstBody += dsPoolProsTxt.toString();

        StringBuilder logProsTxt = new StringBuilder();
        logProsTxt.append("\t\t\tlogPros = new Properties();// 日志配置").append(GlobalConstant.CR);
        Set<String> logProsNames = logPros.stringPropertyNames();
        for (String name : logProsNames){
            String value = logPros.getProperty(name);
            logProsTxt.append("\t\t\tlogPros.put( \""+name+"\" ,  \""+value+"\" );\n");
        }
        firstBody += logProsTxt.toString();
        firstBody += "\t\t\tdataTaskPool = BinDataxBoot.init(sysPros,logPros,dsPoolPros);//初始化任务组执行环境" + GlobalConstant.CR;

        javaTxt = javaTxt.replace(FIRST_BODY,firstBody);

        StringBuilder taskMetaConfigTxt = new StringBuilder();
        taskMetaConfigTxt.append("Properties taskMetaConfig = new Properties();// 每个任务的配置数据").append(GlobalConstant.CR);
        if(!fieldPro.isEmpty()){
            Set<String> strings = fieldPro.stringPropertyNames();
            for (String name : strings){
                String value = fieldPro.getProperty(name);
                taskMetaConfigTxt.append("\t\tconfigValue = get(Fields.In, \""+value+"\" ).getString(r);").append(GlobalConstant.CR);
                taskMetaConfigTxt.append("\t\tconfigValue = StringUtils.getValue(configValue,\"\");").append(GlobalConstant.CR);
                taskMetaConfigTxt.append("\t\ttaskMetaConfig.put( \""+name+"\" , configValue );").append(GlobalConstant.CR);
                taskMetaConfigTxt.append(GlobalConstant.CR);
                taskMetaPros.remove(name);
            }
        }
        if(!taskMetaPros.isEmpty()){
            Set<String> strings = taskMetaPros.stringPropertyNames();
            for (String name : strings){
                String value = taskMetaPros.getProperty(name);
                taskMetaConfigTxt.append("\t\ttaskMetaConfig.put( \""+name+"\" ,  \""+value+"\" );").append(GlobalConstant.CR);
            }
        }
        taskMetaConfigTxt.append("\t\t// 添加任务到任务线程组 执行").append(GlobalConstant.CR);
        taskMetaConfigTxt.append("\t\tBinDataxBoot.addTask( dataTaskPool , taskMetaConfig , this );").append(GlobalConstant.CR);

        javaTxt = javaTxt.replace(EXE_ROW_BODY,taskMetaConfigTxt.toString()) + GlobalConstant.CR;

        //加入结束销毁
        StringBuilder stopAllSb = new StringBuilder(javaTxt);
        stopAllSb.append("//如果其他组件停止或者当前转换停止，则停止任务组执行。").append(GlobalConstant.CR);
        stopAllSb.append("public void stopAll() {").append(GlobalConstant.CR);
        stopAllSb.append("\t\tif( dataTaskPool != null ){").append(GlobalConstant.CR);
        stopAllSb.append("\t\t\tdataTaskPool.stopAllTask();").append(GlobalConstant.CR);
        stopAllSb.append("\t\t\tdataTaskPool = null;").append(GlobalConstant.CR);
        stopAllSb.append("\t\t}").append(GlobalConstant.CR);
        stopAllSb.append("}").append(GlobalConstant.CR);

        javaTxt = stopAllSb.toString();

        return javaTxt;
    }
}
